package com.lungle.warmdiary.app.view.active.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseListFragment;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.event.ActiveCommentRefreshEvent;
import com.lungle.warmdiary.app.model.response.CampaignComment;
import com.lungle.warmdiary.app.model.response.Comment;
import com.lungle.warmdiary.app.utils.LogUtils;
import com.lungle.warmdiary.app.utils.UIHelper;
import com.lungle.warmdiary.app.view.active.adapter.ActiveCommentAdapter;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 活动-亲测列表
 */
public class ActiveCommentFragment extends BaseListFragment {
    private static final String ACTIVE_COMMENT_SCREEN = "active_comment_screen";
    public static final String VALUE_GROUD = "groupid";
    private String groupid;
    private int status;
    private String sharelink;
    private String sharelinkTitle;

    private String productid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(ACTIVE_COMMENT_SCREEN);
        MobclickAgent.onResume(getActivity());

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(ACTIVE_COMMENT_SCREEN);
        MobclickAgent.onPause(getActivity());

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Override
    protected ListBaseAdapter getListAdapter() {
        ActiveCommentAdapter adapter = new ActiveCommentAdapter();
        adapter.setActivity(getActivity());
        adapter.setFragment(ActiveCommentFragment.this);
        return adapter;
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        super.onItemClick(parent, view, position, id);

        if (TextUtils.isEmpty(productid)) {
            if (mAdapter.getItem(position) instanceof Comment) {
                Comment item = (Comment) mAdapter.getItem(position);
                item.setStatus(status);
                item.setSharelink(sharelink);
                item.setSharelinkTitle(sharelinkTitle);
                UIHelper.showActiveCommentDetail(getActivity(), item);
            }
        } else if (mAdapter.getItem(position) instanceof CampaignComment) {
            CampaignComment item = (CampaignComment) mAdapter.getItem(position);
            item.setCampaignStatus(status);
            item.setShareLink(sharelink);
            item.setShareLinkTitle(sharelinkTitle);
            UIHelper.showActiveCommentDetail(getActivity(), item);
        }
    }

//    @Override
//    protected CommentList parseList(InputStream is) throws Exception {
//        return CommentList.parse(is);
//    }
//
//
//    @Override
//    protected CommentList readList(Serializable seri) {
//        return (CommentList) seri;
//    }

    @Override
    protected void requestData(boolean refresh) {
        sendRequestData();
    }


    @Override
    protected void sendRequestData() {

        if (!TextUtils.isEmpty(productid)) {
            ApiFactory.getUserApi().getCampaignDetailList(productid, 0, mCurrentPage * getPageSize(), getPageSize()).subscribeOn(Schedulers.io())
                    .compose(this.<Result<?>>bindToLifecycle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(mSubscriber);
        } else if (!TextUtils.isEmpty(groupid)) {
            ApiFactory.getUserApi().getActiveCommentList(groupid, mCurrentPage * getPageSize(), getPageSize()).subscribeOn(Schedulers.io())
                    .compose(this.<Result<?>>bindToLifecycle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(mSubscriber);
        }


    }

    @Override
    protected boolean requestDataIfViewCreated() {
        return false;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommentChanged(ActiveCommentRefreshEvent event) {

        productid = event.getProductid();
        LogUtils.printLogD("productid:" + productid);
        groupid = event.getId();
        if (TextUtils.isEmpty(groupid)) return;
        status = event.getStatus();
        sharelink = event.getLink();
        sharelinkTitle = event.getLinkTitle();
        sendRequestData();

    }

}
